﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BingImages
{
    class ThemeDraw
    {
        private themeForm _themeForm;        
        private FlowLayoutPanel _flpPicture;
        private FlowLayoutPanel _flpTheme;
        private ProgressBar _pbPicture;
        private Settings _settings;
        private Folder _folder;
        private Copy _copy;
        private static PictureBox[] _pbxTheme;
        private static int _themeNum;

        public ThemeDraw(themeForm themeForm)
        {
            this._themeForm = themeForm;
            this._flpPicture = this._themeForm.getFlpPicture();
            this._flpTheme = this._themeForm.getFlpTheme();
            this._pbPicture = this._themeForm.getPbPicture();
            this._settings = new Settings();
            this._folder = new Folder();
            this._copy = new Copy(this._themeForm);            
            _pbxTheme = new PictureBox[100];
            _themeNum = 0;
        }

        public void setDraw(string file)
        {
            FileStream fs = null;

            PictureBox pbxPicture = new PictureBox();
            pbxPicture.Size = new Size(131, 121);
            pbxPicture.BorderStyle = BorderStyle.Fixed3D;
            using (fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                pbxPicture.BackgroundImage = new Bitmap(Image.FromStream(fs));
                fs.Close();
            }
            pbxPicture.BackgroundImageLayout = ImageLayout.Stretch;
            pbxPicture.Tag = file;
            pbxPicture.Click += new EventHandler(pbxPicture_Click);
            pbxPicture.MouseEnter += new EventHandler(pbxPicture_MouseEnter);

            this._flpPicture.Invoke((MethodInvoker)(() =>
            {
                this._flpPicture.Controls.Add(pbxPicture);
            }));
        }

        public void setTheme(string file)
        {
            FileStream fs = null;

            _pbxTheme[_themeNum] = new PictureBox();
            _pbxTheme[_themeNum].Size = new Size(86, 75);
            _pbxTheme[_themeNum].BorderStyle = BorderStyle.Fixed3D;
            using (fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                _pbxTheme[_themeNum].BackgroundImage = new Bitmap(Image.FromStream(fs));
            }
            _pbxTheme[_themeNum].BackgroundImageLayout = ImageLayout.Stretch;
            _pbxTheme[_themeNum].Tag = _themeNum + "|" + file;
            _pbxTheme[_themeNum].Name = "2-PictureBox" + _themeNum;
            _pbxTheme[_themeNum].Click += new EventHandler(pbxTheme_Click);
            _pbxTheme[_themeNum].MouseEnter += new EventHandler(pbxTheme_MouseEnter);

            this._flpTheme.Controls.Add(_pbxTheme[_themeNum]);
            
            _themeNum++;
        }

        public void init(string monthNumber)
        {
            string archivePath = "";

            this.removeControls();

            this._folder.deleteFiles("temp", "*.png");
            this._folder.deleteFiles("temp", "*.jpg");

            if (monthNumber == DateTime.Now.Month.ToString("d2"))
            {
                this._copy.copyFiles(this._settings.path, this._folder.tempPath, "*.png");
                this._copy.copyPicture(this._settings.path, this._folder.tempPath, "*.png", new Size(131, 121));
            }
            else
            {
                Extract extract = new Extract(this._themeForm);
                archivePath = this._settings.path + @"\Archives";

                DirectoryInfo archiveDirectory = new DirectoryInfo(archivePath);
                FileInfo[] archiveFiles = archiveDirectory.GetFiles("*-" + monthNumber + "-*.zip");

                // Extraction des fichiers dans le dossier "temp"
                extract.setExtract(archiveFiles[0].FullName, monthNumber);
            }
        }

        private void removeControls()
        {
            #region Example : AGAIN
            //Again:
        //    foreach (Control ctrl in this._themeForm.getFlpPicture().Controls)
        //    {
        //        if ((ctrl) is PictureBox)
        //        {
        //            this._themeForm.getFlpPicture().Controls.Remove(ctrl);
        //        }
        //    }
        //    if (this._themeForm.getFlpPicture().Controls.Count > 0)
        //    {
        //        goto Again;
            //    }
            #endregion
            this._flpPicture.Controls.Clear();
        }

        public void clear()
        {
            this._flpPicture.Controls.Clear();

            if (_pbxTheme[0] != null)
            {
                foreach (PictureBox pbx in _pbxTheme)
                {
                    if (pbx != null)
                    {
                        pbx.BackgroundImage.Dispose();
                        pbx.BackgroundImage = null;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }

        #region EVENTS
        private void pbxPicture_Click(object sender, EventArgs e)
        {
            string pngFile = "";
            string jpgFile = "";
            string fileName = "";
            string filePath = "";

            jpgFile = (string)((PictureBox)sender).Tag;
            fileName = Path.GetFileNameWithoutExtension(jpgFile);
            pngFile = fileName + ".png";
            pngFile = this._folder.tempPath + "\\" + pngFile;

            this._copy.copyFile(pngFile, this._folder.themePath);
            this._copy.copyFile(jpgFile, this._folder.themePath);

            filePath = Path.Combine(this._folder.themePath, fileName + ".jpg");

            this.setTheme(filePath);
        }

        private void pbxPicture_MouseEnter(object sender, EventArgs e)
        {
            this._flpPicture.Focus();
        }

        private void pbxTheme_Click(object sender, EventArgs e)
        {
            string tag = "";
            string[] tagSearch = null;
            Control pbxControl = null;
            int pbxNum = 0;
            string pngFile = "";
            string jpgFile = "";

            tag = ((PictureBox)sender).Tag.ToString();
            tagSearch = tag.Split(new string[] { "|" }, StringSplitOptions.None);

            pbxNum = Convert.ToInt32(tagSearch[0]);
            jpgFile = tagSearch[1];
            pngFile = Path.GetFileNameWithoutExtension(jpgFile);
            pngFile = pngFile + ".png";

            // Recherche du ChekBox (pbxControl)
            pbxControl = this._flpTheme.Controls.Find("2-PictureBox" + pbxNum, true)[0];

            // Suppression de l'image
            this._folder.deleteFile(this._folder.themePath, pngFile);
            this._folder.deleteFile(this._folder.themePath, jpgFile);

            // Suppression de "pbxControl"
            this._flpTheme.Controls.Remove(pbxControl);

            foreach (PictureBox iPic in _pbxTheme)
            {
                if (iPic != null)
                {
                    if (iPic.Parent != null)
                    {
                        this._flpTheme.Controls.Add(iPic);
                    }
                }
                else
                {
                    break;
                }
            }
        }

        private void pbxTheme_MouseEnter(object sender, EventArgs e)
        {
            this._flpTheme.Focus();
        }
        #endregion
    }
}
